package com.sinoglobal.ningxia.activity.vip;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.TextUtil;
import com.sinoglobal.ningxia.utils.ValidUtil;

/**
 * @author lms
 * @title: RegistActivity.java
 * @package com.sinoglobal.ningxia.activity.vip
 * @description: 注册页面
 * @date 2014-10-17 上午10:24:49
 */
public class Regist extends BaseActivity {
    // 下一步
    private TextView next;
    // 输入手机号
    private EditText phoneEdit;
    // 手机号字符串
    private String phoneStr;
    // 传递的activity名称
    private String activityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_regist);
        openChenJInShi();//开启沉浸式
        init();
        setOnClickListener();

    }

    // 添加点击事件的监听
    private void setOnClickListener() {
        //下一步
        next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                phoneStr = phoneEdit.getText().toString().trim();
                String validPhone = ValidUtil.validPhone(phoneStr);
                if (!TextUtil.stringIsNull(validPhone)) {
                    showShortToastMessage(validPhone);
                    return;
                }
                sendCode();
                pushActivity(Regist.this);
            }
        });
    }

    // 向手机发送验证码
    public void sendCode() {
        new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getResources().getString(R.string.sending), true, true) {

            @Override
            public RootVo before(Void... params) throws Exception {
                return RemoteImpl.getInstance().getRegistUserCode(phoneStr);
            }

            @Override
            public void after(RootVo result) {
                if (null == result) { // 数据为空，显示reLoad
                    showReLoading();
                    return;
                }
                if (Code.CODE_SUCCESS != result.getCode()) {
                    showShortToastMessage(result.getMessage());
                    return;
                } else {
                    Intent intent = new Intent(Regist.this, CodeInput.class);
                    intent.putExtra("activity", "regist");
                    intent.putExtra("phoneNum", phoneStr);
                    intent.putExtra("activityName", activityName);
                    startActivity(intent);
                    pushActivity(Regist.this);
                }
            }

            @Override
            public void exception() {
                dismissWaitingDialog();
                showShortToastMessage(getResources().getString(R.string.loadFail));
            }
        }.execute();
    }

    private void init() {
        titleView.setText(R.string.regist_title);
        next = (TextView) findViewById(R.id.next_tv);
        phoneEdit = (EditText) findViewById(R.id.regist_edit_phone);
        activityName = getIntent().getStringExtra("activityName");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
